home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_pas / tsfaqp31.zip / FAQPAS.TXT next >
Text File  |  1996-04-28  |  71KB  |  1,481 lines

  1. From ts@uwasa.fi Sun Apr 28 00:00:00 1996
  2. Subject: FAQPAS.TXT contents
  3.  
  4.                              Copyright (c) 1993-1996 by Timo Salmi
  5.                                                All rights reserved
  6.  
  7. FAQPAS.TXT Frequently (and not so frequently) asked Turbo Pascal
  8. questions with Timo's answers. The items are in no particular order.
  9.  
  10. You are free to quote brief passages from this file provided you
  11. clearly indicate the source with a proper acknowledgment.
  12.  
  13. Comments and corrections are solicited. But if you wish to have
  14. individual Turbo Pascal consultation, please post your questions to
  15. a suitable Usenet newsgroup like news:comp.lang.pascal.borland. It
  16. is much more efficient than asking me by email. I'd like to help,
  17. but I am very pressed for time. I prefer to pick the questions I
  18. answer from the Usenet news. Thus I can answer publicly at one go if
  19. I happen to have an answer. Besides, newsgroups have a number of
  20. readers who might know a better or an alternative answer. Don't be
  21. discouraged, though, if you get a reply like this from me. I am
  22. always glad to hear from fellow Turbo Pascal users.
  23.  
  24. If you are an experienced Turbo Pascal programmer with Turbo Pascal
  25. material you would like to circulate publicly world-wide, you are
  26. welcome to submit your material to the Garbo MS-DOS archives at the
  27. University of Vaasa. To do that you *FIRST* must *CAREFULLY* read
  28. the uploading instructions and the formal requirements in the file
  29. ftp://garbo.uwasa.fi/pc/UPLOAD.INF. You are also welcome to contact
  30. me by email for these instructions if you are not familiar with
  31. downloading them from the Garbo archives.
  32.  
  33. ....................................................................
  34. Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
  35. Moderating at ftp:// & http://garbo.uwasa.fi archives  193.166.120.5
  36. Department of Accounting and Business Finance  ; University of Vaasa
  37. ts@uwasa.fi http://uwasa.fi/~ts BBS 961-3170972; FIN-65101,  Finland
  38.  
  39. --------------------------------------------------------------------
  40.  1) How do I disable or capture the break key in Turbo Pascal?
  41.  2) How do I get a printed documentation of my students' TP runs?
  42.  3) What is the code for the weekday of a given date?
  43.  4) Need a program to format Turbo Pascal source code consistently
  44.  5) Can someone give me advice for writing a tsr program?
  45.  6) Why can't I read / write the com ports?
  46.  7) What are interrupts and how to use them in Turbo Pascal?
  47.  8) Should I upgrade my Turbo Pascal version?
  48.  9) How do I execute an MS-DOS command from within a TP program?
  49. 10) How is millisecond timing done?
  50. 11) How can I customize the text characters to my own liking?
  51. 12) How to find the files in a directory and subdirectories?
  52. 13) I need a power function but there is none in Turbo Pascal.
  53. 14) How can I create arrays that are larger than 64 kilobytes?
  54. 15) How can I test that the printer is ready?
  55. 16) How can I clear the keyboard type-ahead buffer?
  56. 17) How can I utilize expanded memory (EMS) in my programs?
  57. 18) How can I obtain the entire command line?
  58. 19) How do I redirect text from printer to file in my TP program?
  59. 20) Turbo Pascal is for wimps. Use standard Pascal or C instead?
  60. 21) How do I turn the cursor off?
  61. 22) How to find all roots of a polynomial?
  62. 23) What is all this talk about "Pascal homework on the net"?
  63. 24) How can I link graphics drivers directly into my executable?
  64. 25) How can I trap a runtime error?
  65. --------------------------------------------------------------------
  66.  
  67. Unless otherwise stated the answers cover versions 4.0, 5.0, 5.5,
  68. 6.0 and 7.0 (real mode). The Q&As are not for Turbo Pascal version 3
  69. or earlier. Objects, TVision, Windows, Delphi, etc are not covered.
  70. (I do not use them myself.)
  71. --------------------------------------------------------------------
  72.  
  73. From ts@uwasa.fi Sun Apr 28 00:00:01 1996
  74. Subject: Disabling or capturing the break key
  75.  
  76. 1. *****
  77.  Q: I don't want the Break key to be able to interrupt my TP
  78. programs. How is this done?
  79.  Q2: I want to be able to capture the Break key in my TP program.
  80. How is this done?
  81.  Q3: How do I detect if a certain key has been pressed? (Often, how
  82. do I detect, for example, if the CursorUp key has been pressed?)
  83.  Q4: How do I detect if a cursor key or a function key has been
  84. pressed?
  85.  
  86.  A: This set of frequently asked questions is basically a case of
  87. RTFM (read the f*ing manual). But this feature is, admittedly, not
  88. very prominently displayed in the Turbo Pascal reference. (As a
  89. general rule we should not use the newsgroups as a replacement for
  90. our possibly missing manuals, but enough of this line.)
  91.    I'll only explain Q and Q2. The other two, Q3 and Q4 should be
  92. evident from the example code.
  93.    There is a CheckBreak variable in the Crt unit, which is true by
  94. default. To turn it off use
  95.      uses Crt;
  96.      :
  97.      CheckBreak := false;
  98.      :
  99. Besides turning off break checking this enables you to capture the
  100. pressing of the break key as you would capture pressing ctrl-c. In
  101. other words you can use e.g.
  102.      :
  103.   procedure TEST;
  104.   var key : char;
  105.   begin
  106.     repeat
  107.       if KeyPressed then
  108.         begin
  109.           key := ReadKey;
  110.           case key of
  111.              #0 : begin
  112.                     key := ReadKey;
  113.                     case key of
  114.                       #59 : write ('F1 ');
  115.                       #72 : write ('CursUp ');   { Detecting these }
  116.                       #75 : write ('CursLf ');   { is often asked! }
  117.                       #77 : write ('CursRg ');
  118.                       #80 : write ('CursDn ');
  119.                       else write ('0 ', ord(key), ' ');
  120.                     end; {case}
  121.                   end;
  122.              #3 : begin    {ctrl-c or break}
  123.                     writeln ('Break');
  124.                     halt(1);
  125.                   end;     { Terminate the program, or whatever }
  126.             #27 : begin
  127.                     write ('<esc> ');
  128.                     exit;  { Exit test, continue program }
  129.                   end;
  130.             else write (key, ' ');
  131.           end; {case}
  132.         end; {if}
  133.     until false;
  134.   end;  (* test *)
  135.      :
  136. IMPORTANT: Don't test the ctrl-break feature just from within the TP
  137. IDE, because it has ctlr-break handler ("interceptor") of its own
  138. and may confuse you into thinking that ctrl-break cannot be
  139. circumvented by the method given above.
  140.   The above example has a double purpose. It also shows the
  141. rudiments how you can detect if a certain key has been pressed. This
  142. enables you to give input without echoing it to the screen, which is
  143. a later FAQ in this collection.
  144.   This is, however, not all there can be to break checking, since
  145. the capturing is possible only at input time. It is also possible to
  146. write a break handler to interrupt a TP program at any time. For
  147. more details see Ohlsen & Stoker, Turbo Pascal Advanced Techniques,
  148. Chapter 7. (For the bibliography, see FAQPASB.TXT in this same FAQ
  149. collection).
  150.  
  151.  A2: This frequent question also elicits one of the most frequent
  152. false answers. It is often suggested erroneously that the relevant
  153. code would be
  154.   uses dos;
  155.   SetCBreak(false);
  156. This is not so. It confuses MS-DOS and TP break checking with each
  157. other. SetCBreak(false) will _*NOT*_ disable the Ctrl-Break key for
  158. your Turbo Pascal program. What it does is "Sets the state of
  159. Ctrl-Break checking in DOS". SetCBreak sets the state of Ctrl+Break
  160. checking in DOS. When off (False), DOS only checks for Ctrl+Break
  161. during I/O to console, printer, or communication devices. When on
  162. (True), checks are made at every system call."
  163.    This item goes to shows how important it is carefully to check
  164. one's code and facts before claiming something.
  165.  
  166.  A3: Using the "CheckBreak := false;" method is not the only
  167. alternative, however. Here is an example code for disabling
  168. Ctrl-Break and Ctrl-C with interrupts
  169.   uses Dos;
  170.   var OldIntr1B : pointer;  { Ctrl-Break address }
  171.       OldIntr23 : pointer;  { Ctrl-C interrupt handler }
  172.       answer    : string;   { For readln test }
  173.   {$F+}
  174.   procedure NewIntr1B (flags,cs,ip,ax,bx,cx,dx,si,di,ds,es,bp : word);
  175.             Interrupt;
  176.   {$F-} begin end;
  177.   {$F+}
  178.   procedure NewIntr23 (flags,cs,ip,ax,bx,cx,dx,si,di,ds,es,bp : word);
  179.             Interrupt;
  180.   {$F-} begin end;
  181.   begin
  182.     GetIntVec ($1B, OldIntr1B);
  183.     SetIntVec ($1B, @NewIntr1B);   { Disable Ctrl-Break }
  184.     GetIntVec ($23, OldIntr23);
  185.     SetIntVec ($23, @NewIntr23);   { Disable Ctrl-C }
  186.     writeln ('Try breaking, disabled');
  187.     readln (answer);
  188.     SetIntVec ($1B, OldIntr1B);    { Enable Ctrl-Break }
  189.     SetIntVec ($23, OldIntr23);    { Enable Ctrl-C }
  190.     writeln ('Try breaking, enabled');
  191.     readln (answer);
  192.     writeln ('Done');
  193.   end.
  194. --------------------------------------------------------------------
  195.  
  196. From ts@uwasa.fi Sun Apr 28 00:00:02 1996
  197. Subject: Directing output also to printer
  198.  
  199. 2. *****
  200.  Q: I want to have a printed documentation of my students' Turbo
  201. Pascal program exercises. How is all input and output directed also
  202. to the printer?
  203.  
  204.  A1: Use a screen capturing program to put everything that comes
  205. onto the screen into a file, and print the file. See FAQPROGS.TXT in
  206. ftp://garbo.uwasa.fi/pc/ts/tsfaqn44.zip (or whatever version number
  207. is the current) for more about these programs. Available by
  208. anonymous FTP or mail server from garbo.uwasa.fi.
  209.  
  210.  A2: See the code in TSPAS.NWS (item: Redirecting writes to the
  211. printer) in the ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or whatever
  212. is the current version number) Turbo Pascal units package (70 = 40,
  213. 50, 55, 60, or 70 depending on your TP version). Alternatively use
  214. USECON and USEPRN routines in the TSUNTG unit of the same package.
  215.  
  216.      +------------------------------------------+
  217.      ! To get these and other packages given as !
  218.      !   /dir/subdir/name                       !
  219.      ! see the instructions in PD2ANS.TXT       !
  220.      +------------------------------------------+
  221.  
  222.  A3: But the really elegant solution to the problem of getting a
  223. logfile (or a printed list) of a Turbo Pascal run is to rewrite the
  224. write(ln) and read(ln) device driver functions. In itself writing
  225. such driver redirections is very advanced Turbo Pascal programming,
  226. but when the programming has once been done, the system is extremely
  227. easy to use as many times as you like. It goes like this. The driver
  228. redirections are programmed into a unit (say, tpulog or tpuprn). All
  229. that is needed after that is to include the following uses statement
  230. into the program (the target program) which has to be logged:
  231.       uses TPULOG;    ( or )    uses TPUPRN;
  232. This is all there is to it. Just adding one simple line to the
  233. target program. (If you call any other units, "uses tpulog" must
  234. come AFTER the system units (e.g. Dos), but BEFORE any which you may
  235. define yourself!)
  236.    The reason that I have named two units here instead of just one
  237. in the above example is that the preferred log for the target
  238. program may be a logfile or the printer. The better solution of
  239. these two is to use the logfile option, and then print it. The
  240. reason is simple. If the target program itself prints something,
  241. your printout will look confused.
  242.    The logging also has obvious limitations. It works for standard
  243. input and output (read(ln) and write(ln)) only. 1) It does not
  244. support graphics, in other words it is for the textmode. 2) It does
  245. not support direct (Crt) screen writes. 3) And, naturally it only
  246. shows the input and output that comes to the screen. Not any other
  247. input or output, such as from or to a file. 4) Furthermore, you are
  248. not allowed to reassign input or output. Statements like assign
  249. (output, '') will result in a crash, because the rewritten output
  250. device redirections are invalidated by such statements. 5) The
  251. device on the default drive must not be write protected, since else
  252. the logfile cannot be written to it. 6) It does not work for Turbo
  253. Pascal 4.0. Despite these restrictions, the method is perfectly
  254. suited for logging students' Turbo Pascal escapades.
  255.    It is advisable first to test and run your target program without
  256. "tpulog", so that if you get any strange errors you'll know whether
  257. they are caused by the logging.
  258.    Where to get such a unit. The code can be found in Michael
  259. Tischer (1990), Turbo Pascal Internals, Abacus, Section 4.2. Next a
  260. few of my own tips on this unit Tischer calls Prot. 1) The code is
  261. in incorrect order. The code that is listed on pages 142 - 145 goes
  262. between pages 139 and 140. 2) You can change the logfile name (const
  263. prot_name) to lpt1 for a printed list of the target program run. In
  264. that case it is advisable to include a test for the online status of
  265. the printer within Tischer's unit. 3) I see no reason why the two
  266. lines in Tischer's interface section couldn't be transferred to the
  267. implementation section. Why have any global definitions?  But all in
  268. all, it works like magic!
  269.  
  270.  A4: From: abcscnuk@csunb.csun.edu (Naoto Kimura (ACM))
  271. Subject: Re: Printing a log of students' exercises revisited
  272. To: ts@uwasa.fi
  273. Date: Fri, 2 Nov 90 20:52:03 pdt
  274. [Reproduced with Naoto's kind permission]
  275. By the way, several months ago, I had submitted a file (nktools.zip)
  276. file on SimTel that contains sources to a unit (LOGGER), which
  277. allows logging of I/O going through the standard input and output
  278. files, while still being able to use the program interactively.  I
  279. believe that I also submitted a copy to your site.  It was something
  280. I put together for use by students here at California State
  281. University at Northridge.  The source works equally well in all
  282. presently available versions of Turbo Pascal.
  283. The only requirements are that
  284.  * you place it as one of the last entries in the USES clause.  If
  285.    there is anything that redirects the standard input and output
  286.    file variables, you should put that unit before my unit in the
  287.    USES clause, so that it can see the I/O stream.
  288.  * Don't use the GotoXY and similar screen display control
  289.    procedures in the Crt unit and expect it to come out the same way
  290.    you had it on the display.  Since all my unit does is just
  291.    capture the I/O stream to send it through the normal channels and
  292.    also to the log file, all screen control information is not sent
  293.    to the log file.
  294.  * All I/O you want logged should go through the standard input and
  295.    output file variables.
  296.  * Don't close the standard input and output file variables, because
  297.    it will cause problems.  Basically, as far as I have checked, it
  298.    just causes the logging to stop at that point.
  299. --------------------------------------------------------------------
  300.  
  301. From ts@uwasa.fi Sun Apr 28 00:00:03 1996
  302. Subject: Code to give the weekday of a date
  303.  
  304. 3. *****
  305.  Q: I want code that gives the weekday of the given date.
  306.  
  307.  A1: There is a WKDAYFN function in my Turbo Pascal units collection
  308. ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or whatever version number
  309. is the latest, and where 70 is 40 50 55 60 and 70) Turbo Pascal
  310. units collection to give the modern weekday based on Zeller's
  311. congruence. Available by anonymous FTP or mail server from
  312. garbo.uwasa.fi. Also you can find a more extensive Julian and
  313. Gregorian weekday algorithm with source code in Dr.Dobb's Journal,
  314. June 1989, p. 148. Furthermore Press & Flannery & al (1986),
  315. Numerical Recipes, Cambridge University Press, present a weekday
  316. code. The Numerical Recipes codes are available as
  317. ftp://garbo.uwasa.fi/pc/turbopas/nrpas13.zip (big, 404k!).
  318.  
  319.  A2: Some will recommend the following kludge. Store the current
  320. date, change it, and let MS-DOS get you the weekday. Don't use it!
  321. It is a bad suggestion. On top of being sloppy programming, there
  322. are several snags.  The trick works only for years 1980-2079. A
  323. crash the program may leave the clock at a wrong date. And even if
  324. multitasking is rare, in a multitasking environment havoc may result
  325. for the other tasks. And you may have a TSR that requires the
  326. correct date, etc.
  327. --------------------------------------------------------------------
  328.  
  329. From ts@uwasa.fi Sun Apr 28 00:00:04 1996
  330. Subject: Pretty printers (or uniform code)
  331.  
  332. 4. *****
  333.  Q: Where can I find a program that formats my (or my students')
  334. Turbo Pascal code in a consistent matter.
  335.  
  336.  A: What you are asking for is often called "a pretty printer".
  337. TurboPower Software's (the usual disclaimer applies) commercial
  338. Turbo Analyst has a facility for this with many options. There are
  339. also PD and shareware pretty printers, such as
  340.  :
  341.  16830 Nov 28 1989 ftp://garbo.uwasa.fi/pc/turbopas/ppdk50.zip
  342.  ppdk50.zip Pascal Prettypringting Program, tweak, D.Kirschbaum
  343.  :
  344.  10015 Mar 29 1991 ftp://garbo.uwasa.fi/pc/turbopas/ppp.zip
  345.  ppp.zip Pretty Print Pascal, with Turbo Pascal 5+ source, M.Bless
  346.  :
  347.  38021 Nov 5 1994 ftp://garbo.uwasa.fi/pc/turbopas/bp7sb104.zip
  348.  bp7sb104.zip Borland and TP Source Beautifier, crippleware, J.Ferincz
  349.  :
  350.  25100 Jul 4 1992 ftp://garbo.uwasa.fi/pc/turbopas/epb232.zip
  351.  epb232.zip Ed's Pascal Beautifier, E.Lee
  352.  :
  353. and others at garbo.uwasa.fi available by anonymous FTP or mail
  354. server. See ftp://garbo.uwasa.fi/pc/INDEX.ZIP for the list of the
  355. files.
  356. --------------------------------------------------------------------
  357.  
  358. From ts@uwasa.fi Sun Apr 28 00:00:05 1996
  359. Subject: How to write TSR programs
  360.  
  361. 5. *****
  362.  Q: Can someone give me advice for writing a tsr program?
  363.  
  364.  A: Writing a terminate and stay resident program can be considered
  365. advanced programming and is beyond the scope of an electronic
  366. message with limited space. Instead, here are some references to
  367. Turbo Pascal books and papers which have a coverage of the subject.
  368. Stephen O'Brien, Turbo Pascal, The Complete Reference, Chapter 16;
  369. Stephen O'Brien, Turbo Pascal, Advanced Programmer's Guide, Chapter
  370. 6; Michael Tischer, Turbo Pascal Internals, Chapter 11 (a definite
  371. bible of TP programming!); Michael Tischer (1992), PC Intern System
  372. Programming, Chapter 32; Michael Yester, Using Turbo Pascal, Chapter
  373. 19; Kent Pottebaum, "Creating TSR Programs with Turbo Pascal", Dr.
  374. Dobb's Journal, May 1989 and June 1989; Kris Jamsa, Dos Power User's
  375. Guide, pp. 649-; Edward Mitchell (1993), Borland Pascal Developer's
  376. Guide, Section "Writing TSRs", pp. 370-400, with 778 lines of sample
  377. code.
  378.    Also see example code files like
  379. ftp://garbo.uwasa.fi/pc/turboobj/tsrhelp.zip,
  380. ftp://garbo.uwasa.fi/pc/turbopa45/tess-5.zip,
  381. ftp://garbo.uwasa.fi/pc/turbopa45/tsrunit.zip,
  382. ftp://garbo.uwasa.fi/pc/turbopa7/pptsr10.zip,
  383. ftp://garbo.uwasa.fi/pc/turbopa7/tsrsrc35.zip,
  384. ftp://garbo.uwasa.fi/pc/turbopas/deltsr.zip,
  385. ftp://garbo.uwasa.fi/pc/turbopas/tp4_tsr.zip.
  386.    Furthermore, you should see the TSR.SWG examples in the fine SWAG
  387. (SourceWare Archival Group's) collection of TP sources. Available
  388. from the /pc/turbopas directory at Garbo. For the current references
  389. to the SWAG files see ftp://garbo.uwasa.fi/pc/INDEX.ZIP.
  390. --------------------------------------------------------------------
  391.  
  392. From ts@uwasa.fi Sun Apr 28 00:00:06 1996
  393. Subject: Programming com ports
  394.  
  395. 6. *****
  396.  Q: Why can't I read / write the com ports.
  397.  
  398.  A: Com port programming (most often writing telecommunication
  399. programs) is much much more complicated than simply trying to use
  400.   write (com, whatever);
  401.   read  (com, whatever);
  402. This is a very advanced subject (frankly, beyond me), and the best
  403. way to learn is to try to obtain some code to show you how. One
  404. place to look at is Turbo Pascal text-books (I have a long list of
  405. them at garbo.uwasa.fi archives in my collection of Turbo Pascal
  406. units ftp://garbo.uwasa.fi/pc/ts/tsfaqp3470.zip. There also is an
  407. example by David Rind in ftp://garbo.uwasa.fi/pc/pd2/faquote.zip.
  408. Another source is International FidoNet pascal conference at some
  409. bulletin board near you. The conference has had some very good
  410. discussions in it. (No, I don't have them stored for distribution,
  411. nor any further information.) Some files you might wish to look at:
  412. ftp://garbo.uwasa.fi/pc/turbopas/comm_tp5.zip and comtty30.zip.
  413.    Furthermore, you should see the COMM.SWG examples in the fine
  414. SWAG (SourceWare Archival Group's) collection of TP sources.
  415. Available from the /pc/turbopas directory at Garbo. For the current
  416. references to the SWAG files see ftp://garbo.uwasa.fi/pc/INDEX.ZIP.
  417. --------------------------------------------------------------------
  418.  
  419. From ts@uwasa.fi Sun Apr 28 00:00:07 1996
  420. Subject: Primers to interrupt programming
  421.  
  422. 7. *****
  423.  Q: What are interrupts and how to use them in Turbo Pascal?
  424.  
  425.  A: An interrupt is a signal to the processor from a program, a
  426. hardware device, or the processor itself, to suspend temporarily
  427. what the program is doing, and to perform a routine that is stored
  428. in the operating system. There are 256 such interrupt routines, with
  429. many subservices stored in memory at locations, which are given in
  430. the so called interrupt table. Turbo Pascal (somewhat like C) has a
  431. special keyword Intr, and a predefined variable registers (in the
  432. Dos unit) to access these interrupt routines. One way of looking at
  433. them is as Turbo Pascal (complicated lowlevel) subroutines that are
  434. already there ready for you to call.
  435.    A detailed description of interrupt routines is way beyond a
  436. single message with limited space. Instead, I shall give a simple
  437. example, and good references to the subject. (For a somewhat more
  438. comprehensive description of what an interrupt is, see INTERRUP.PRI
  439. in Ralf Brown's ftp://garbo.uwasa.fi/pc/programming/inter49b.zip.)
  440.      :
  441.      uses Dos;
  442.      (* This procedure turns on the border color for CGA and VGA *)
  443.      procedure BORDER (color : byte);
  444.      var regs : registers;  (* Predeclared in the Dos unit *)
  445.      begin
  446.        FillChar (regs, SizeOf(regs), 0);  (* A precaution *)
  447.        regs.ax := $0B00;    (* Service number *)
  448.        regs.bh := $00;      (* Subservice number *)
  449.        regs.bl := color;
  450.        Intr ($10, regs);    (* ROM BIOS video driver interrupt *)
  451.      end;  (* border *)
  452.    If you are new the subject and / or want ideas on the most useful
  453. interrupts in Turbo Pascal programming, Ben Ezzel (1989),
  454. Programming the IBM User Interface Using Turbo Pascal, is definitely
  455. the best reference to look at. There are also many other good
  456. references for a novice interrupt user, such as Jamsa & Nameroff,
  457. Turbo Pascal Programmer's Library.
  458.    If you are a more advanced interrupt user you'll find the
  459. following references very useful. Michael Tischer (1990), Turbo
  460. Pascal Internals; Norton & Wilton (1988), The New Peter Norton
  461. Programmer's guide to the IBM PC & PS/2; Ray Duncan (1988), Advanced
  462. MS-DOS Programming; Terry Dettmann (1989), Dos Programmer's
  463. Reference, Second edition, Que. Furthermore, there is an impressive
  464. list of interrupts collected and maintained by Ralf Brown. His
  465. extensive ftp://garbo.uwasa.fi:/pc/programming/inter49a.zip,
  466. inter49b.zip, inter49c.zip, inter49d.zip, inter49e.zip and
  467. inter49f.zip (or whatever are the current versions when you read
  468. this) is available by anonymous FTP or mail server from
  469. garbo.uwasa.fi. A definite must for an advanced user. Also see the
  470. reference to Brown's and Kyle's book in the bibliography at the end
  471. of this FAQ. There is also a good hypertext advanced programmer's
  472. quick reference ftp://garbo.uwasa.fi/pc/programming/helppc21.zip
  473. which you might find useful.
  474.    One more point for Turbo Pascal users. When Borland upgraded from
  475. version 3 to 4.0 quite a number of tasks that needed to be done
  476. using interrupts (such as getting the current time) were included as
  477. normal TP routines. This means that while definitely useful,
  478. interrupt programming is now relevant only in advanced Turbo Pascal
  479. programming. Turbo Pascal 5.0 introduced a few more, but you can
  480. find some of the missing TP 4.0 routines in the compatibility unit
  481. in my ftp://garbo.uwasa.fi/pc/ts/tspa3440.zip TP units collection.
  482. --------------------------------------------------------------------
  483.  
  484. From ts@uwasa.fi Sun Apr 28 00:00:08 1996
  485. Subject: Borland's Turbo Pascal upgrades
  486.  
  487. 8. *****
  488.  Q: Should I upgrade my Turbo Pascal version?
  489.  
  490.  A1: Depends on what version you are using, and for what purposes.
  491. If you are using version 3, the answer is a definite yes. There are
  492. so many useful additions in the later version, including the concept
  493. of units, and a great number of new useful keywords. The only reason
  494. that I can think of for using TP 3 is that it makes .com files
  495. (which reside in one memory segment only) instead of .exe files. As
  496. an accounting and business finance teacher and researcher I've been
  497. somewhat surprised to see postings stating that some users still
  498. have to program in TP 3.0 because their employer doesn't want to
  499. take the cost of upgrading. I find this cost argument ridiculous.
  500. How about some consideration for cost effectiveness and
  501. productivity?
  502.    If you are currently using version 4.0, the most important point
  503. in considering upgrading is the integrated debugger in the later
  504. versions. It is really good, and useful if you write much code.
  505. There are some minor considerations, as well. Later versions contain
  506. some useful routines which 4.0 does not. I have programmed many of
  507. them to be available also for 4.0 in my units collection
  508. ftp://garbo.uwasa.fi/pc/ts/tspa3440.zip (or whatever is the latest
  509. when you read this). Furthermore, I find somewhat annoying that the
  510. executables will always end up in the default directory.
  511.    If you are currently using version 5.0 the rational reasons for
  512. upgrading are needing objects, and a better overlay manager. I have
  513. also version 5.5 myself, but switched back to version 5.0 after I
  514. had some problems with its linking of object files. (This is a false
  515. statement from me, since it turned out that I had made a mistake
  516. myself. My thanks are due to bj_stedm@gould2.bristol-poly.ac.uk
  517. (Bruce Stedman) for questioning this item). Anyway, I don't use nor
  518. need OOP objects (don't confuse linking object files and object
  519. oriented programming here). One further point for 5.5. It has a
  520. better help function than 5.0, and a few more procedures and
  521. predefined constants. The TP 5.5 help includes examples, which can
  522. be even pasted into your program. This is handy.
  523.    The real snag in upgrading (waiving the reasonable cost) is the
  524. fact that the units of the different versions are incompatible. If
  525. you have a large library of units (as I do) you will have to
  526. recompile the lot. This is something that has caused a fair amount
  527. of justifiable flak against an otherwise excellent product.
  528.    A tip. Don't throw away your Turbo Pascal version 3.0 manual, if
  529. you have one. It is of use if you resort to the Turbo3 and Graph3
  530. compatibility units. They give you access e.g. to turtle graphics.
  531.    At the time of first writing this Turbo Pascal 6.0 version had
  532. just been announced. I didn't have it yet myself, but I had been
  533. (correctly) informed that its units are not compatible with the
  534. earlier versions. I now have Turbo Pascal 6.0, and I must say that
  535. my reactions have been disappointment and frustration. This is
  536. probably partly (but not entirely) my own fault, since Turbo Pascal
  537. seems to be headed from a common programming language into a full
  538. professional's specialized tool, with many features I don't know how
  539. to utilize. The only advancement from my point of view really is the
  540. multiple file editing, but I have long had alternative programs for
  541. that. If I used assembler (I don't) I am sure that I would find
  542. useful TP 6.0's potential to include assembler code as such instead
  543. of having to use the cumbersome Inline procedure of entering the
  544. assembler code.
  545.    There is also a Windows Turbo Pascal, as the latest addition to
  546. the plethora. Since I don't use Windows at all, I have no further
  547. information on it.
  548.    I think a pattern is emerging here. Rather than being different
  549. versions of the same product, the consecutive Turbo Pascals are
  550. really different products for different purposes. Version 3.0 was a
  551. simple programming language. Version 4.0 extended it into a full
  552. scale programming modular platform. Version 5.0 introduced the
  553. debugger. And there an advanced hobbyist's path ended. Version 5.5
  554. introduced object oriented programming, which I'm sure is important
  555. for the initiated, but personally I just don't need it even if I
  556. write a lot of programs. And with the 6.0 we go completely out of
  557. the realm of conventional programming into Turbo Pascal visions.
  558. And Windows Turbo Pascal is for a different platform, altogether.
  559.    I find the new integrated user interface of TP 6.0 awkward in
  560. comparison to what was used in the 4.0, 5.0, and 5.5 versions. The
  561. IDE of TP leaves less free memory than the previous versions.
  562. Furthermore TP 6.0 IDE performs frequent disk accesses which cause
  563. slowdowns  making it virtually unusable from a floppy. And I
  564. wonder why Borland didn't at once go all the way to Windows, because
  565. that is what 6.0 really is. An intermediate, incomplete step in that
  566. direction. This means that we have a 5th upgrade in line with
  567. incompatible units. This is aggravating even for a TP fan, isn't it?
  568.    For information on Turbo Pascal version 7.0 and Borland email
  569. contact numbers see ftp://garbo.uwasa.fi/pc/turbopa7/bp7-info.zip.
  570. Also see ftp://garbo.uwasa.fi/pc/turbspec/bp7bugs2.zip by Duncan
  571. Murdoch. Turbo Pascal 7.0 or more extensively Borland Pascal 7.0 is
  572. a full professional's tool, and far beyond for example my moderate
  573. programming needs. To list only a few of the features are protected
  574. mode programming, handling of large programs, very fast compiling,
  575. and a daunting amount of material elbowing its away on one's disk
  576. space if one ever has the patience to look through it all. I would
  577. use the word "overwhelming". But for a serious programmer this is an
  578. impressive and a very worthwhile tool. One should not be misled
  579. skipping it because of my comments which were written from a
  580. hobbyist's point of view. As a general trend in programs, the
  581. well-known columnist John C. Dvorak calls this increasing product
  582. complexity trend "featurism" in PC Computing, May 1993. But TP 7.0
  583. (7.01) has some important features also from a hobbyist's point of
  584. view. So much so that I have finally succumbed to 7.01 myself. I
  585. particularly like the color coding of the keywords, and the TPX
  586. version enabling compiling very large programs (utilizing extended
  587. memory). A very welcome addition are the new keywords break and
  588. continue to exit or recycle a for, while or a repeat loop are.
  589. Besides they make using the outcast goto statements virtually
  590. unnecessary.
  591.  
  592.  A2: From: dmurdoch@watstat.waterloo.edu (Duncan Murdoch),
  593. Newsgroups: comp.lang.pascal. Included with Duncan's kind
  594. permission. [Duncan is one of the most knowledgeable and useful
  595. contributors to the comp.lang.pascal UseNet newsgroup (later
  596. replaced by comp.lang.pascal.borland for TP)].
  597.    One other reason:  there's a bug in the code generator for 4.0
  598. and 5.0 that makes it handle the Extended (10 byte) real type
  599. poorly.  The code generated makes very poor use of the 8 element
  600. internal stack on the coprocessor, so that expressions with lots of
  601. operands like
  602.   e1+e2+e3+e4+e5+e6+e7+e8+e9
  603. always fail, if all the e's are of type extended.  (The generated
  604. code pushes each operand onto the stack, then does all the adds.
  605. It's smarter to push and add them one at a time.)
  606.    This makes it a real pain translating numerical routines from
  607. Fortran, especially since constants are taken to be of type
  608. extended.
  609.    The bug was fixed in 5.5.
  610.  
  611.  A3: From: Bengt Oehman (d92bo@efd.lth.se): A difference between
  612. v4.0 and v5.5 is that you can calculate constants in tp55, but not
  613. in 4.0. I see this as a big advantage. For example:
  614.   CONST MaxW = 10;
  615.         MaxH = 20;
  616.         MaxSize = MaxW*MaxH;
  617.         { or }
  618.         MaxX = 100;
  619.         HalfMaxX = MaxX DIV 2;
  620. cannot be compiled with 4.0.
  621. --------------------------------------------------------------------
  622.  
  623. From ts@uwasa.fi Sun Apr 28 00:00:09 1996
  624. Subject: Shelling from a TP program
  625.  
  626. 9. *****
  627.  Q: How do I execute an MS-DOS command from within a TP program?
  628.  
  629.  A: The best way to answer this question is to give an example.
  630.      {$M 2048, 0, 0}   (* <-- Important *)
  631.      program outside;
  632.      uses dos;
  633.      begin
  634.        write ('Directory call from within TP by Timo Salmi');
  635.        SwapVectors;
  636.        Exec (GetEnv('comspec'), '/c dir *.*');  (* Execution *)
  637.        SwapVectors;
  638.        (* Testing for errors is recommended *)
  639.        if DosError <> 0 then
  640.          writeln ('Dos error number ', DosError)
  641.        else
  642.          writeln ('Mission accomplished, exit code ', DosExitCode);
  643.        (* For DosError and DosExitCode details see the TP manual *)
  644.      end.
  645. Alternatively, take a look at execdemo.pas from demos.arc which
  646. should be on the disk accompanying Turbo Pascal.
  647.    What the above Exec does is that it executes the command
  648. processor. The /c specifies that the command interpreter is to
  649. perform the command, and then stop (not halt).
  650.    I have also seen it asked how one can swap the Turbo Pascal
  651. program to the disk when shelling. It is unnecessary to program that
  652. separately because there is an excellent program to do that for you.
  653. It is ftp://garbo.uwasa.fi/pc/sysutil/shrom24b.zip. Also of interest
  654. to advanced programmers even if in C
  655.  107534 Dec 12 1992 ftp://garbo.uwasa.fi/pc/c-lang/spwno413.zip
  656.  spwno413.zip Disk/XMS Swapping replacement for spawn(), w/C, R.Brown
  657.   Somewhat surprisingly some users have had difficulties with
  658. redirecting shelled output. It is straight-forward. In the above
  659. code one would use, for example
  660.   Exec (GetEnv('comspec'), '/c dir *.* > tmp');
  661. --------------------------------------------------------------------
  662.  
  663. From ts@uwasa.fi Sun Apr 28 00:00:10 1996
  664. Subject: Millisecond timing
  665.  
  666. 10. *****
  667.  Q: How is millisecond timing done?
  668.  
  669.  A: A difficult task, but the facilities are readily available.
  670. TurboPower Software's commercial Turbo Professional (don't confuse
  671. with Borland's Turbo Professional) has a unit for this. (The usual
  672. disclaimer applies). It is called tptimer and is part of the
  673. ftp://garbo.uwasa.fi/pc/turbopas/bonus507.zip package. This one has
  674. been released to the PD. I have also seen a SimTel upload
  675. announcement of a ztimer11.zip for C and ASM, but I have no further
  676. information on that. ftp://garbo.uwasa.fi/pc/turbopas/qwktimer.zip
  677. is another option. It is not quite as accurate as tptimer.
  678.    To test the tptimer unit in bonus507.zip you can use the
  679. following example code
  680.   uses Crt, tptimer;
  681.   var time1, time2 : longint;
  682.   begin
  683.     InitializeTimer;
  684.     time1 := ReadTimer;
  685.     Delay (1356);    (* Or whatever code you wish to benchmark *)
  686.     time2 := ReadTimer;
  687.     RestoreTimer;
  688.     writeln ('Elapsed = ', ElapsedTime (time1, time2)/1000.0 : 0 : 3);
  689.   end.
  690.    It is quite another question when you really need the millisecond
  691. timing. The most common purpose for millisecond timing is testing
  692. the efficiency of alternative procedures and functions, right? The
  693. way I compare mine is simple. I call the procedures or functions I
  694. want to compare for speed, say, a thousand times in a loop.  And
  695. test this for elapsed time. This way the normal resolution (18.2
  696. cycles per second) of the system clock becomes sufficient. This is
  697. accurate enough for the comparisons.
  698.      var elapsed : real; i : word;
  699.      elapsed := TIMERFN;  (* e.g. from /pc/ts/tspa3455.zip *)
  700.      for i := 1 to 1000 do YOURTEST;  (* Try out the alternatives *)
  701.      elapsed := TIMERFN - elapsed;
  702.      writeln ('Elapsed : ', elapsed : 0 : 2);
  703. Incidentally, if you want to make more elaborate evaluations of the
  704. efficiency of your code, Borland's Turbo Profiler is a useful tool.
  705. (The usual disclaimer naturally applies.)
  706. --------------------------------------------------------------------
  707.  
  708. From ts@uwasa.fi Sun Apr 28 00:00:11 1996
  709. Subject: Text font customizing
  710.  
  711. 11. *****
  712.  Q: How can I customize the text characters to my own liking?
  713.  
  714.  A: As far as I know, text-mode characters are hard-coded, and
  715. cannot be customized at all unless you have an EGA or VGA adapter.
  716. But you can always retrieve the bitmap information for the ascii
  717. characters from your PC.
  718.    The bitmap table for the lower part of the character set (0-127)
  719. starts at memory position $F000 and ends at $FA6E. The upper part is
  720. not at a fixed memory location. The pointer to the memory address of
  721. upper part of the ascii table (provided that graftabl has been
  722. loaded) is at an address $007C. One way of saying this is that the
  723. segment address of the upper part's memory location is at $007E, and
  724. its offset at $007C.
  725.    Going into more details is beyond the scope of this posting. If
  726. you want more information see Michael Tischer (1992), PC Intern
  727. System Programming, "Selecting and Programming Fonts", pp. 197-210.
  728. It also has information on a remotely related task of using sprites
  729. (pp. 305-373), a concept familiar from the days of the Commodore 64
  730. games programming. For another reference to customizing characters
  731. see Kent Porter (1987), Stretching Turbo Pascal, Chapter 12, and
  732. Kent Porter & Mike Floyd (1990), Stretching Turbo Pascal. Version
  733. 5.5. Revised Edition. Brady, Chapter 11.
  734.    If you are interested in a demonstration of utilizing the
  735. bitmapped character information (no source code available), take a
  736. look at the demo in the garbo.uwasa.fi anonymous FTP archives file
  737. ftp://garbo.uwasa.fi/pc/ts/tsdemo16.zip (or whatever version number
  738. is current).
  739.    Turbo Pascal also supports what is called stroked fonts (the
  740. .chr) files which draw characters instead of bitmapping them. The
  741. user should be able to write one's own .chr definitions, but I have
  742. no experience nor information on how this can be done.
  743.    There is something called bgikit10.zip which has facilities for
  744. making fonts and adding graphics drivers. The problem is that I
  745. cannot make it publicly available, since I think that it is not PD.
  746. I am still missing the information. Unfortunately, it is not even
  747. the only case where I encountered the fact that Borland does not
  748. seem at all interested in the UseNet users' queries about the status
  749. and distributability of their material.
  750.    (From Leonard Erickson Leonard.Erickson@f51.n105.z1.fidonet.org
  751. Well, you can *also* do it if you have a Hercules Graphics Card Plus
  752. or Hercules InColor card (as far as I know, the only cards that
  753. implemented Hercules RamFont 'standard'). And you can modify the
  754. upper 128 characters on a CGA card. BTW, the RamFont cards are
  755. *nice* pity it appeared too late to become a standard. It's a *lot*
  756. more flexible than EGA/VGA fonts (I can have several *dozen* fonts
  757. resident).)
  758. --------------------------------------------------------------------
  759.  
  760. From ts@uwasa.fi Sun Apr 28 00:00:12 1996
  761. Subject: Finding files in TP
  762.  
  763. 12. *****
  764.  Q: How to find the files in a directory AND subdirectories?
  765.  
  766.  A: Writing a program that goes through the files of the directory,
  767. and all the subdirectories below it, is based on Turbo Pascal's file
  768. finding commands and recursion. This is universal whether you are
  769. writing, for example, a directory listing program, or a program that
  770. deletes, say, all the .bak files, or some other similar task.
  771.    To find (for listing or other purposes) the files in a directory
  772. you need above all the FindFirst and FindNext keywords, and testing
  773. the predefined file attributes. You make these a procedure, and call
  774. it recursively. If you want good examples with source code, please
  775. see PC World, April 1989, p. 154; Kent Porter & Mike Floyd (1990),
  776. Stretching Turbo Pascal. Version 5.5. Revised Edition, Chapter 23;
  777. Michael Yester (1989), Using Turbo Pascal, p. 437; Michael Tischer
  778. (1992), PC Intern System Programming, pp. 796-798.
  779.    The simple (non-recursive) example listing all the read-only
  780. files in the current directory shows the rudiments of the principle
  781. of Using FindFirst, FindNext, and the file attributes, because some
  782. users find it hard to use these keywords. Also see the code in the
  783. item "How to establish if a name refers to a directory or not?" of
  784. this same FAQ collection you are now reading.
  785.     uses Dos;
  786.     var FileInfo : SearchRec;
  787.     begin
  788.       FindFirst ('*.*', AnyFile, FileInfo);
  789.       while DosError = 0 do
  790.         begin
  791.           if (FileInfo.Attr and ReadOnly) > 0 then
  792.             writeln (FileInfo.Name);
  793.           FindNext (FileInfo);
  794.         end;
  795.     end.  (* test *)
  796.  
  797.  A2: While we are on the subject related to FindFirst and FindNext,
  798. here are two useful examples:
  799.  
  800. (* Number of files in a directory (not counting directories) *)
  801. function DFILESFN (dirName : string) : word;
  802. var nberOfFiles  : word;
  803.     FileInfo     : searchRec;
  804. begin
  805.   if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  806.   dirName := dirName + '*.*';
  807.   {}
  808.   nberOfFiles := 0;
  809.   FindFirst (dirName, AnyFile, FileInfo);
  810.   while DosError = 0 do
  811.     begin
  812.       if ((FileInfo.Attr and VolumeId) = 0) then
  813.         if (FileInfo.Attr and Directory) = 0 then
  814.           Inc (nberOfFiles);
  815.       FindNext (FileInfo);
  816.     end; {while}
  817.   dfilesfn := nberOfFiles;
  818. end;  (* dfilesfn *)
  819.  
  820. (* Number of immediate subdirectories in a directory *)
  821. function DDIRSFN (dirName : string) : word;
  822. var nberOfDirs : word;
  823.     FileInfo    : searchRec;
  824. begin
  825.   if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  826.   dirName := dirName + '*.*';
  827.   {}
  828.   nberOfDirs:= 0;
  829.   FindFirst (dirName, AnyFile, FileInfo);
  830.   while DosError = 0 do
  831.     begin
  832.       if ((FileInfo.Attr and VolumeId) = 0) then
  833.         if (FileInfo.Attr and Directory) > 0 then
  834.           if (FileInfo.Name <> '.') and (FileInfo.Name <> '..') then
  835.             Inc (nberOfDirs);
  836.       FindNext (FileInfo);
  837.     end; {while}
  838.   ddirsfn := nberOfDirs;
  839. end;  (* ddirsfn *)
  840. --------------------------------------------------------------------
  841.  
  842. From ts@uwasa.fi Sun Apr 28 00:00:13 1996
  843. Subject: A generic power function code for TP
  844.  
  845. 13. *****
  846.  Q: I need a power function but there is none in Turbo Pascal.
  847.  
  848.  A: Pascals do not have an inbuilt power function. You have to write
  849. one yourself. The common, but non-general method is defining
  850.    function POWERFN (number, exponent : real) : real;
  851.      begin
  852.        powerfn := Exp(exponent*Ln(number));
  853.      end;
  854. To make it general use:
  855.    (* Generalized power function by Prof. Timo Salmi *)
  856.    function GENPOWFN (number, exponent : real) : real;
  857.    begin
  858.      if (exponent = 0.0) then
  859.        genpowfn := 1.0
  860.      else if number = 0.0 then
  861.        genpowfn := 0.0
  862.      else if abs(exponent*Ln(abs(number))) > 87.498 then
  863.        begin writeln ('Overflow in GENPOWFN expression'); halt; end
  864.      else if number > 0.0 then
  865.        genpowfn := Exp(exponent*Ln(number))
  866.      else if (number < 0.0) and (Frac(exponent) = 0.0) then
  867.        if Odd(Round(exponent)) then
  868.          genpowfn := -GENPOWFN (-number, exponent)
  869.        else
  870.          genpowfn :=  GENPOWFN (-number, exponent)
  871.      else
  872.        begin writeln ('Invalid GENPOWFN expression'); halt; end;
  873.    end;  (* genpowfn *)
  874. On the lighter side of things an extract from an answer of mine in
  875. the late comp.lang.pascal UseNet newsgroup:
  876.  >anyone point out why X**Y is not allowed in Turbo Pascal?
  877.    The situation in TP is a left-over from standard
  878.    Pascal. You'll recall that Pascal was originally
  879.    devised for teaching programming, not for
  880.    something as silly and frivolous as actually
  881.    writing programs.  :-)
  882. --------------------------------------------------------------------
  883.  
  884. From ts@uwasa.fi Sun Apr 28 00:00:14 1996
  885. Subject: Arrays > 64K
  886.  
  887. 14. *****
  888.  Q: How can I create arrays that are larger than 64 kilobytes?
  889.  
  890.  A: Turbo Pascal does not directly support the so-called huge arrays
  891. but you can get by this problem with a clever use of pointers as
  892. presented in Kent Porter, "Handling Huge Arrays", Dr.Dobb's Journal,
  893. March 1988. In this method you point to an element of a two
  894. dimensional array using a^[row].col^[column]. The idea involves too
  895. much code and explanation to be repeated here, so you'll have to see
  896. the original reference. But I know from my own experience, that the
  897. code works like magic. (The code is available from garbo.uwasa.fi
  898. archives as ftp://garbo.uwasa.fi/pc/turbopas/ddj8803.zip). Kent
  899. Porter, "Huge Arrays Revisited", Dr.Dobb's Journal, October 1988,
  900. presents the extension of the idea to huge virtual arrays. (Virtual
  901. arrays mean arrays that utilize disk space).
  902.    Another possibility is using TurboPower Software's (the usual
  903. disclaimer applies) commercial Turbo Professional (don't confuse
  904. with Borland's Turbo Professional) package. It has facilities for
  905. huge arrays, but they involve much more overhead than Kent Porter's
  906. excellent method.
  907.  
  908. (* =================================================================
  909.    My code below is based on a UseNet posting in the late
  910.    comp.lang.pascal by Naji Mouawad nmouawad@watmath.waterloo.edu.
  911.    Naji's idea was for a vector, my adaptation is for a
  912.    two-dimensional matrix. The realization of the idea is simpler
  913.    than the one presented by Kent Porter in Dr.Dobb's Journal, March
  914.    1988. (Is something wrong, this is experimental.)
  915.    ================================================================= *)
  916.    {}
  917.    const maxm = 150;
  918.          maxn = 250;
  919.    {}
  920.    type BigVectorType = array [1..maxn] of real;
  921.         BigMatrixType = array [1..maxm] of ^BigVectorType;
  922.    {}
  923.    var BigAPtr : BigMatrixType;
  924.    {}
  925.    (* Create the dynamic variables *)
  926.    procedure MAKEBIG;
  927.    var i          : word;
  928.        heapNeeded : longint;
  929.    begin
  930.      heapNeeded := maxm * maxn * SizeOf(real) + maxm * 4 + 8196;
  931.      if (MaxAvail <= heapNeeded) then
  932.        begin writeln ('Out of memory'); halt; end;
  933.      for i := 1 to maxm do New (BigAPtr[i]);
  934.    end;  (* makebig *)
  935.    {}
  936.    (* Test that it works *)
  937.    procedure TEST;
  938.    var i, j : word;
  939.    begin
  940.      for i := 1 to maxm do
  941.        for j := 1 to maxn do
  942.          BigAPtr[i]^[j] := i * j;
  943.      {}
  944.      writeln (BigAPtr[5]^[7] : 0:0);
  945.      writeln (BigAPtr[maxm]^[maxn] : 0:0);
  946.    end;  (* test *)
  947.    {}
  948.    (* The main program *)
  949.    begin
  950.      writeln ('Big arrays test by Prof. Timo Salmi, Vaasa, Finland');
  951.      writeln;
  952.      MAKEBIG;
  953.      TEST;
  954.    end.
  955. (For a better test of the heap than in MAKEBIG see Swan (1989), pp.
  956. 462-463.)
  957. --------------------------------------------------------------------
  958.  
  959. From ts@uwasa.fi Sun Apr 28 00:00:15 1996
  960. Subject: Testing printer status
  961.  
  962. 15. *****
  963.  Q: How can I test that the printer is ready?
  964.  
  965.  A: Strictly speaking there is no guaranteed way to detect the
  966. printer status on a PC. As Brian Key Brian@fantasia.demon.co.uk
  967. wrote "Any book dealing with the PC BIOS support of a printer will
  968. quickly show you that there is no hardware definition which deals
  969. with the printer power status.  It simply wasn't designed (and I use
  970. the word loosely!) into the IBM hardware specs."
  971.  
  972.    The usually advocated method in Turbo Pascal is to test the
  973. status of regs.ah after a call to interrupt 17 Hex (the parallel
  974. port driver interrupt), service 02:
  975.       regs.dx := PrinterNumber;  (* LPT1 = 0 *)
  976.       regs.ah := $02;            (* var regs : registers, uses DOS *)
  977.       Intr ($17,regs);           (* Interrupt 17 Hex *)
  978.       status := regs.ah          (* var status : byte *)
  979. But this is not a good method since the combinations of the status
  980. bits which indicate a ready state can vary from printer to printer
  981. and PC to PC. If you want a list of the status bits, see e.g. Ray
  982. Duncan (1988), Advanced MS-DOS Programming, p. 587. For an example
  983. of a code using interrupt 17 Hex see Douglas Stivison (1986), Turbo
  984. Pascal Library, pp. 118-120. Also see Michael Yester (1989), Using
  985. Turbo Pascal, pp. 494-495.
  986.    The more generic alternative is to try to write a #13 to the
  987. printer having the i/o checking off, that is, while {$I-} is in
  988. effect, and testing the IOResult. But then you must first alter the
  989. printer retry times default (and restore it afterwards). Else the
  990. method can take up to a minute instead of an immediate response.
  991. Also, you must have set the FileMode for LPT1 appropriately (and
  992. restore it afterwards). Sounds a bit complicated, but you don't have
  993. to do all this yourself. There is a boolean function "LPTONLFN Get
  994. the online status of the first parallel printer" for this purpose in
  995. my ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or whatever version
  996. number is the latest) Turbo Pascal units collection available by
  997. anonymous FTP or mail server from garbo.uwasa.fi.
  998.  
  999.  A2: One potential, somewhat advanced solution is to use the Device
  1000. Driver Control (IOCTL) information. Here is the code.
  1001.   uses Dos;
  1002.   function PRNSTAFN : boolean;
  1003.   var regs   : registers;
  1004.       handle : ^word;
  1005.       f      : file;
  1006.   begin
  1007.     prnstafn := false;
  1008.     if swap(Dosversion) < $0200 then exit;  { At least MS-DOS 2.0 }
  1009.     Assign (f, 'prn');                 { Printer }
  1010.     Reset (f);
  1011.     FillChar (regs, SizeOf(regs), 0);  { Just to make sure }
  1012.     regs.ah := $44;                    { Function $44 }
  1013.     regs.al := $07;                    { Subfunction $07 }
  1014.     handle  := @f;                     { Establish a file handle }
  1015.     regs.bx := handle^;
  1016.     Msdos (regs);                      { Call interrupt $21 }
  1017.     Close (f);
  1018.     if regs.flags and FCarry <> 0 then exit;  { Is the carry flag set? }
  1019.     if regs.al <> $FF then exit;       { regs.al = $FF signals success}
  1020.     prnstafn := true;
  1021.   end;  (* prnstafn *)
  1022.   {}
  1023.   begin
  1024.     if PRNSTAFN then writeln ('Printer ready')
  1025.       else writeln ('Printer not ready');
  1026.     readln;
  1027.   end.
  1028.  
  1029.  A3: Another advanced method is using ports. This solution is based
  1030. on a posting by Joerg Kunze joerg@ang-physik.uni-kiel.de. A warning.
  1031. Do not experiment with the port parameters unless you know exactly
  1032. what you are doing. A serious loss of data might follow.
  1033.   function PRNON : boolean;
  1034.   var dr : word absolute $40:$08;
  1035.   begin
  1036.     prnon := port[dr+1] and $18=$18;
  1037.   end;
  1038. --------------------------------------------------------------------
  1039.  
  1040. From ts@uwasa.fi Sun Apr 28 00:00:16 1996
  1041. Subject: Clearing the keyboard buffer
  1042.  
  1043. 16. *****
  1044.  Q: How can I clear the keyboard type-ahead buffer?
  1045.  
  1046.  A: Three methods are usually suggested for solving this problem.
  1047. a) The first is to use something like
  1048.      uses Crt;
  1049.      while KeyPressed do ReadKey;
  1050. This kludge-type method has the disadvantage of requiring the Crt
  1051. unit. Also, in connection with procedures relying on ReadKey for
  1052. input, it may cause havoc on the programs logic.
  1053. b) The second method accesses directly the circular keyboard buffer
  1054.      var head : word absolute $0040:$001A;
  1055.          tail : word absolute $0040:$001C;
  1056.      procedure FLUSHKB; begin head := tail; end;
  1057. For a slightly different formulation of the same method see Michael
  1058. Tischer (1992), PC Intern System Programming, p. 462.
  1059. c) The third method is to call interrupt 21Hex (the MS-DOS
  1060. interrupt) with the ax register set as $0C00. This method has the
  1061. advantage of not being "hard-coded" like the second method, and thus
  1062. should be less prone to incompatibility.
  1063. --------------------------------------------------------------------
  1064.  
  1065. From ts@uwasa.fi Sun Apr 28 00:00:17 1996
  1066. Subject: Utilizing expanded memory
  1067.  
  1068. 17. *****
  1069.  Q: How can I utilize expanded memory (EMS) in my programs?
  1070.  
  1071.  A: I have no experience (yet?) on this subject myself, but I can
  1072. give you a list of references: Michael Tischer (1990), Turbo Pascal
  1073. Internals, Abacus, Chapter 9; Michael Tischer (1992), PC Intern
  1074. System Programming, Chapter 12; Stephen O'Brien (1988), Turbo
  1075. Pascal, Advanced Programmer's Guide, Borland-Osborne, Chapter 4;
  1076. Chris Ohlsen & Gary Stoker (1989), Turbo Pascal Advanced Techniques,
  1077. Que, Chapter 11, Robert Jourdain (1992), Programmer's Problem
  1078. Solver, 2nd ed., Brady Publishing, pp. 68-87, and, maybe most
  1079. importantly, Dorfman & Neuberger, Turbo Pascal Memory Management
  1080. Techniques (with lots of code).
  1081.    Furthermore, Turbo Pascal delivery disks (at least 5.0) contain a
  1082. demos.arc archive which includes an ems.pas file.
  1083. --------------------------------------------------------------------
  1084.  
  1085. From ts@uwasa.fi Sun Apr 28 00:00:18 1996
  1086. Subject: Capturing the entire command line
  1087.  
  1088. 18. *****
  1089.  Q: How can I obtain the entire command line (spaces and all)?
  1090.  
  1091.  A: ParamCount and ParamStr are for parsed parts of the command line
  1092. and cannot be used to get the command line exactly as it was. See
  1093. what happens if you try to capture
  1094.   "Hello.   I'm here"
  1095. you'll end up with a false number of blanks. For obtaining the
  1096. command line unaltered use
  1097.   type CommandLineType = string[127];
  1098.   var  CommandLinePtr  : ^CommandLineType;
  1099.   begin
  1100.     CommandLinePtr := Ptr(PrefixSeg, $80);
  1101.     writeln (CommandLinePtr^);
  1102.   end;
  1103. A warning. If you want to get this correct (the same goes for TP's
  1104. own ParamStr and ParamCount) apply them early in your program. At
  1105. least they must be used before any disk I/O takes place!
  1106. :
  1107. A related example demonstrating a function giving the number of
  1108. characters on the command line
  1109.   function CMDNBRFN : byte;
  1110.   var paramPtr : ^byte;
  1111.   begin
  1112.     paramPtr := Ptr (PrefixSeg, $80);
  1113.     cmdnbrfn := paramPtr^
  1114.   end;  (* cmdnbrfn *)
  1115. For the contents of the Program Segment Prefix (PSP) see Tischer,
  1116. Michael (1992), PC Intern System Programming, p. 753.
  1117. --------------------------------------------------------------------
  1118.  
  1119. From ts@uwasa.fi Sun Apr 28 00:00:19 1996
  1120. Subject: Redirecting from printer to file
  1121.  
  1122. 19. *****
  1123.  Q: How do I redirect text from printer to file in my TP program?
  1124.  
  1125.  A: Simple. This is done in Turbo Pascal by using the assign command
  1126. (think what the word 'assign' implies). Here is a simple example of
  1127. the idea.
  1128.   uses Printer;
  1129.   begin
  1130.     assign (lst, 'printer.log');
  1131.     rewrite (lst);
  1132.     writeln (lst, 'Hello world');
  1133.     close (lst);
  1134.   end.
  1135. --------------------------------------------------------------------
  1136.  
  1137. From ts@uwasa.fi Sun Apr 28 00:00:20 1996
  1138. Subject: Turbo Pascal users are just wimps
  1139.  
  1140. 20. *****
  1141.  Q: Turbo Pascal is for wimps. Why don't you use standard Pascal or
  1142. better still why don't you use C?
  1143.  
  1144.  A: These kinds of "real-programmers" statements often reflect what
  1145. is called self-over-others attitude, and they are a part of a kind
  1146. of a programming lore or cult. Basically, these attitudes waive the
  1147. simple fact that one should select one's tools according to the task
  1148. at hand, not vice versa. On the other hand one's productivity is
  1149. usually best when being able to use tools which one is familiar and
  1150. comfortable with. (Note however that the real-programmer's lore is
  1151. not really interested in producing results.)
  1152.    In very rough terms there are two attitudes to programming
  1153. languages. They can be seen as tools for writing applications, or
  1154. (by surprisingly many) as ends themselves.
  1155.    If we first look at standard Pascal (versus Turbo Pascal),
  1156. considering the language primary and its usage secondary is common.
  1157. This results from the history of Pascal, since as we all know it was
  1158. originally meant as a means for teaching programming concepts, not
  1159. at all for writing applications. But because Pascal turned out to be
  1160. useful also for writing applications, it has been extended for some
  1161. operating systems, most notably MS-DOS (Turbo Pascal) and VAX/VMS
  1162. (VAX Pascal). Both remedy a lot of flaws from the application
  1163. programmer's point of view. Most importantly they have a true file
  1164. I/O interface, and enhanced string handling. Turbo Pascal (the more
  1165. generic of these two) clearly draws from the structure and ideas of
  1166. advanced BASICs (and vice versa). While in standard Pascal the
  1167. language is an end by itself, for Turbo Pascal the only relevant
  1168. issue is its usefulness for writing applications.
  1169.    One problem that one encounters when moving away from standard
  1170. Pascal is the problem of portability. This is a truly serious
  1171. problem, since most often extensive rewriting is necessary from
  1172. converting say Turbo Pascal to, say, Unix Pascal. I have taken Unix
  1173. Pascal as the extreme example, since Unix Pascal is almost nothing
  1174. but the standard Pascal having no useful file I/O.
  1175.    If one considers C, its best aspect from applications point of
  1176. view is portability, and its strength for system programming. But it
  1177. is not an easy language to learn. Proponents of C also often have
  1178. the tendency discussed above, that is seeing the language as
  1179. primary, and its utilization as secondary. Now why this tendency,
  1180. not only for C, but in general? I've had the opportunity of writing
  1181. programs starting from the late 1960's, and one observation I have
  1182. made, and often propounded the view is that it is not writing code
  1183. that is the really difficult part. What is really difficult it is
  1184. coming up with good and original ideas for programs to write. I see
  1185. applications as primary, and the tools as secondary. As to Turbo
  1186. Pascal, I've written in many languages (including Cobol, Fortran,
  1187. several Basics and Pascals, and command languages) and I like Turbo
  1188. Pascal because it is one of the most convenient and flexible tools
  1189. for writing the kind of applications that I usually write and
  1190. distribute for the Public Domain. That is I use Turbo Pascal because
  1191. I'm comfortable with it in writing applications, and have thus
  1192. gathered a very useful modular library for it over the years, not
  1193. because of any inherent value attached to Turbo Pascal per se.
  1194.  
  1195.  A2: Another, a somewhat resembling line is made up by the arguments
  1196. about standards in Pascal which were recycled in the late
  1197. comp.lang.pascal time after time. Very often they end up with
  1198. purists vs pragmatists arguing about the true or imaginary viles of
  1199. using GOTOs. I find all this somewhat futile, although I understand
  1200. the academic nature of the background. As you'll recall, Pascal was
  1201. first developed for academic teaching programming concepts, not for
  1202. any practical programming. That came later, and the ensuing
  1203. popularity of Pascal in practical applications must have come as a
  1204. surprise way back then. I admit being biased in not sympathizing
  1205. with Pascal standard stalwarts. I am far more interested in getting
  1206. the job done than in defending a barren orthodoxy.
  1207.    Since Turbo Pascal version 7.0 introduced the "break" and
  1208. "continue" keywords to handle jumps in loops, GOTOs are much easier
  1209. to avoid without undue complications.
  1210. --------------------------------------------------------------------
  1211.  
  1212. From ts@uwasa.fi Sun Apr 28 00:00:21 1996
  1213. Subject: Turning off the cursor
  1214.  
  1215. 21. *****
  1216.  Q: How do I turn the cursor off?
  1217.  
  1218.  A: The usually advocated trick for turning the cursor off is to
  1219. equate the lower and the upper scan line of the cursor as explained
  1220. e.g. in Stephen O'Brien (1988), Turbo Pascal, Advanced Programmer's
  1221. Guide.
  1222.      uses Dos;
  1223.      var regs : registers;
  1224.      begin
  1225.        regs.ax := $0100;   (* Service $01 *)
  1226.        regs.cl := $20;     (* Top scan line *)
  1227.        regs.ch := $20;     (* Bottom scan line *)
  1228.        Intr ($10, regs);   (* ROM BIOS video driver interrupt *)
  1229.      end;
  1230. To turn the cursor back on this (and many other) sources suggest
  1231. setting regs.ch and regs.cl as 12 and 13 for mono screen, and 6 and
  1232. 7 for others.
  1233.    This is not a good solution since it is equipment dependent, and
  1234. may thus produce unexpected results. Better to store the current
  1235. scan line settings, and turn off the cursor bit. Below is the code
  1236. from ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or whatever version
  1237. number is the latest) available by anonymous FTP from garbo.uwasa.fi
  1238. archives. The general idea is that regs.ch bit 5 toggles the cursor
  1239. on / off state. Thus to set the cursor off, apply
  1240.   regs.ch := regs.ch or $20;    (* $20 = 00100000 *)
  1241. and to set it on, apply
  1242.   regs.ch := regs.ch and $DF;   (* $DF = 11011111 *)
  1243. (* From TSUNTE unit, which also has a CURSON procedure *)
  1244. procedure CURSOFF;
  1245. var regs : registers;
  1246. begin
  1247.   FillChar (regs, SizeOf(regs), 0);  (* Initialize, a precaution *)
  1248.   {... find out the current cursor size (regs.ch, regs.cl) ...}
  1249.   regs.ah := $03;
  1250.   regs.bh := $00;    (* page 1, superfluous because of FillChar *)
  1251.   Intr ($10, regs);  (* ROM BIOS video driver interrupt *)
  1252.   {... turn off the cursor without changing its size ...}
  1253.   regs.ah := $01;                   (* Below are bits 76543210 *)
  1254.   regs.ch := regs.ch or $20;  (* Turn on bit 5; $20 = 00100000 *)
  1255.   Intr ($10, regs);
  1256. end;  (* cursoff *)
  1257.  
  1258.  A2: A comment from Leonard Erickson leonard@qiclab.scn.rain.com.
  1259. Reprinted with permission. There's a *reason* those sources don't
  1260. suggest storing the current scan line settings. On IBM Monochrome
  1261. Display Adapters (MDA), Hercules Graphics Cards, and the various
  1262. clones of both, the "read cursor start and end scan lines" function
  1263. *always* returns the same values. And those values are almost never
  1264. the actual settings. Most cards return 6 & 7. Some return 12 & 13.
  1265. But they return these values even if the cursor has been set to
  1266. something else.
  1267.    So you are *still* stuck with checking the hardware type if the
  1268. screen is in mode 7.
  1269.    See the Interrupt list for details on this mess.
  1270.  
  1271.  A3: Another solution that has been suggested is putting the cursor
  1272. outside the screen.  But you can't do this with the Crt's GotoXY
  1273. procedure, since it ignores off screen positions, as observed by
  1274. Luiz Marques luiz.marques%mandic@ibase.org.br. You'll need to use
  1275. video interrupt, that is $10, function $02. Fair enough, but
  1276. somewhat complicated. Besides, how do you write on the screen if the
  1277. cursor position is off it?
  1278.  
  1279.  A4: This snippet of disabling the cursor at hardware level was
  1280. posted to comp.lang.pascal (now news:comp.lang.pascal.borland) by
  1281. JAB@ib.rl.ac.uk. Corrections due to John Stockton. John also points
  1282. out that this probably needs a VGA to work.
  1283.   procedure turn_off_cursor;
  1284.   var num : word;
  1285.   begin
  1286.     port[$03D4]:=$0A; num:=port[$03D5];
  1287.     port[$03D4]:=$0A; port[$03D5]:=num or 32;
  1288.   end;
  1289.   {}
  1290.   procedure turn_on_cursor;
  1291.   var num : word;
  1292.   begin
  1293.     port[$03D4]:=$0A; num:=port[$03D5];
  1294.     port[$03D4]:=$0A; port[$03D5]:=num and not 32;
  1295.   end;
  1296.   {}
  1297.   procedure toggle_cursor;
  1298.   var num : word;
  1299.   begin
  1300.     port[$03D4]:=$0A; num:=port[$03D5];
  1301.     port[$03D4]:=$0A; port[$03D5]:=num xor 32;
  1302.   end;
  1303.  
  1304.  A5: (Not to be taken seriously). Simple, turn off your computer and
  1305. the cursor stops showing :-).
  1306. --------------------------------------------------------------------
  1307.  
  1308. From ts@uwasa.fi Sun Apr 28 00:00:22 1996
  1309. Subject: Finding the roots of a polynomial
  1310.  
  1311. 22. *****
  1312.  Q: How to find all roots of a polynomial?
  1313.  
  1314.  A: If you need the code, see Turbo Pascal Numerical Toolbox and/or
  1315. Press & Flannery & Teukolsky & Vetterling (1986), Numerical Recipes,
  1316. The Art of Scientific Computing, Cambridge University Press. The
  1317. Numerical Recipes codes are available as /pc/turbopas/nrpas13.zip
  1318. (big, 404k!). If you just need to solve such a task (without code
  1319. available), get ftp://garbo.uwasa.fi/pc/ts/tsnum13.zip (or whatever
  1320. version number is the latest) from garbo.uwasa.fi archives by
  1321. anonymous FTP or mail server.
  1322. --------------------------------------------------------------------
  1323.  
  1324. From ts@uwasa.fi Sun Apr 28 00:00:23 1996
  1325. Subject: Pascal homework on the net
  1326.  
  1327. 23. *****
  1328.  Q: What is all this talk about "Pascal homework on the net"?
  1329.  
  1330.  A: This is one of the subjects that seems to pop up at regular
  1331. intervals, cause some heated exchange for awhile, and then die down
  1332. again leaving some users harboring warranted or unwarranted grudges.
  1333.    Some posters to comp.lang.pascal (later comp.lang.pascal.borland)
  1334. have been very concerned of the possibility that the questions posed
  1335. on the net are related to students' homework assignments. I don't
  1336. have any unequivocal answers or a clear-cut stand on this question,
  1337. just some comments.
  1338.    The most important task of a newsgroup like comp.lang.pascal.borland
  1339. is the exchange of information between the users. If you think that
  1340. what you are going to post is interesting and useful to the group,
  1341. that should be your topmost criterion.
  1342.    If it is really a student that wants his/her work done on the net
  1343. (how do we know anyway?) also consider the following fact. Being
  1344. able to use a newsgroup amounts to having learned at least something
  1345. about using computers, and that is something per se.
  1346.    Even if the student may short-sightedly not realize it, providing
  1347. ALL the code for a student's homework is detrimental to the student,
  1348. since it is she/he that foregoes understanding what he/she is doing.
  1349. The group should not condone outright cheating. Being (partly) a
  1350. teacher myself, I understand also this view.
  1351.    If a student is stuck with a problem in his/her code, I don't see
  1352. any real harm in helping out, especially if the problem has general
  1353. interest. Instructing is what teaching is about, anyway, isn't it?
  1354.    But, on the other hand, I must admit that I find a it rather
  1355. flagrant if a posting asks for something of the kind "I have to
  1356. complete my term assignment to write a function plotter by the end
  1357. of this month. Send me the code, since I'm too busy with my other
  1358. exams to write it myself" (a true quote).
  1359.    Finally, let's not jump to premature conclusions about anyone's
  1360. questions.  That's what most often triggers off a vicious circle of
  1361. flaming.
  1362. --------------------------------------------------------------------
  1363.  
  1364. From ts@uwasa.fi Sun Apr 28 00:00:24 1996
  1365. Subject: Linking bgi drivers into executables
  1366.  
  1367. 24. *****
  1368.  Q: How can I link graphics drivers directly into my executable?
  1369.  
  1370.  A: This is a complicated, yet a very useful task, because then you
  1371. won't need any separate graphics drivers (or fonts) to go separately
  1372. along with your program. Unfortunately, Turbo Pascal documentation
  1373. on this task is a bit confusing.
  1374.    1) The very first step is to get the necessary files from the
  1375. Turbo Pascal disks to your working directory. To start with, you'll
  1376. need binobj.exe and all the .bgi files.
  1377.    2) Run the following commands (best to place them in a batch,
  1378. call it e.g. makeobj.bat):
  1379.      binobj cga.bgi cga CGADriverProc
  1380.      binobj egavga.bgi egavga EGAVGADriverProc
  1381.      binobj herc.bgi herc HercDriverProc
  1382.      binobj pc3270.bgi pc3270 PC3270DriverProc
  1383.      binobj att.bgi att ATTDriverProc
  1384.      rem binobj ibm8514.bgi 8514 IBM8514DriverProc
  1385.    3) Get drivers.pas from the Turbo Pascal disk and compile it with
  1386. Turbo Pascal. Now you have a drivers.tpu unit which contains all the
  1387. graphics drivers.
  1388.    4) Now you won't need the .bgi and the .obj files any more. You
  1389. may delete them from your working directory.
  1390.    5) Write your graphics program in the usual manner. But before
  1391. putting your program in the graphics mode use the following
  1392. procedure if you want to link e.g. the EGAVGA graphics driver
  1393. directly into your executable. (Link just the driver(s) you'll need,
  1394. since the drivers take up a lot of space.)
  1395.      uses Graph, Drivers;
  1396.      :
  1397.      procedure EGAVGA2EXE;
  1398.      begin
  1399.        if RegisterBGIdriver(@EGAVGADriverProc) < 0 then
  1400.          begin
  1401.            writeln ('EGA/VGA: ', GraphErrorMsg(GraphResult));
  1402.            halt(1);
  1403.          end;
  1404.      end; (* egavga2exe *)
  1405.      :
  1406.    Linking the .bgi and .chr drivers is also covered in Swan (1989),
  1407. Mastering Turbo Pascal 5.5 pp. 355-359 and Mitchell (1993), Borland
  1408. Pascal Developer's Guide , pp. 221-229.
  1409.    If you have Turbo Pascal 7.0 its help function gives you an
  1410. example code. One way of getting at it is the following. In the
  1411. Turbo Pascal IDE (that is in the editor) type RegisterBGIdriver.
  1412. Then place the cursor on it and press alt-F1 for help of that
  1413. keyword. Press alt-F10 and select "Copy example". Press first <ESC>
  1414. then alt-F10 and select Paste. The example code is pasted within
  1415. your program for you to study.
  1416.    Incidentally, although this is a slightly different matter, you
  1417. can link any data material into your executable. See Stephen
  1418. O'Brien, (1988), Turbo Pascal, Advanced Programmer's Guide, pp. 31 -
  1419. 35 for more details.
  1420. --------------------------------------------------------------------
  1421.  
  1422. From ts@uwasa.fi Sun Apr 28 00:00:25 1996
  1423. Subject: Trapping runtime errors
  1424.  
  1425. 25. *****
  1426.  Q: How can I trap a runtime error?
  1427.  
  1428.  A: What you are probably asking for is a method writing a program
  1429. termination routine of your own. To do this, you have to replace
  1430. Turbo Pascal's ExitProc with your own customized exec procedure.
  1431. Several Turbo Pascal text books show ho to do this. See e.g. Tom
  1432. Swan (1989), Mastering Turbo Pascal 5.5, Third edition, Hayden
  1433. Books, pp. 440-454; Michael Yester (1989), Using Turbo Pascal, Que,
  1434. pp. 376-382; Stephen O'Brien (1988), Turbo Pascal, Advanced
  1435. Programmer's Guide, pp. 28-30; Tom Rugg & Phil Feldman (1989), Turbo
  1436. Pascal Programmer's Toolkit, Que, pp. 510-515. Here is an example
  1437.   var OldExitProcAddress : Pointer;
  1438.       x : real;
  1439.   {$F+} procedure MyExitProcedure; {$F-}
  1440.   begin
  1441.     if ErrorAddr <> nil then
  1442.       begin
  1443.         writeln ('Runtime error number ', ExitCode, ' has occurred');
  1444.         writeln ('The error address in decimal is ',
  1445.                   Seg(ErrorAddr^):5,':',Ofs(ErrorAddr^):5);
  1446.         writeln ('That''s all folks, bye bye');
  1447.         ErrorAddr := nil;
  1448.         ExitCode  := 0;
  1449.       end;
  1450.     {... Restore the pointer to the original exit procedure ...}
  1451.     ExitProc := OldExitProcAddress;
  1452.   end;  (* MyExitProcedure *)
  1453.   (* Main *)
  1454.   begin
  1455.     OldExitProcAddress := ExitProc;
  1456.     ExitProc := @MyExitProcedure;
  1457.     x := 7.0; writeln (1.0/x);
  1458.     x := 0.0; writeln (1.0/x);   {The trap}
  1459.     x := 7.0; writeln (4.0/x);   {We won't get this far}
  1460.   end.
  1461. :
  1462. Actually, I utilize this idea in my /pc/ts/tspa3470.zip Turbo Pascal
  1463. units collection, which includes a TSERR.TPU. If you put TSERR in
  1464. your program's uses statement, all the run time errors will be given
  1465. verbally besides the usual, cryptic error number. That's all there
  1466. is to it. That is, the inclusion to the uses statement to your main
  1467. program (if you have the program in several units) is all you have
  1468. to do to enable this handy feature.
  1469. :
  1470. Hans.Siemons@f149.n512.z2.fidonet.org notes "This line:
  1471.     ExitProc := OldExitProcAddress;
  1472. should IMHO never be placed at the end of your exit handler. If for
  1473. one reason or another your own handler should cause a runtime error,
  1474. it would go in an endless loop. If the first statement restores the
  1475. exit chain, this can never happen. I do agree that is not very
  1476. likely that your exit handler produces any runtime error, but it
  1477. performs I/O, and since it is located in a FAQ, people are bound to
  1478. use, and maybe extend it with more tricky stuff."
  1479. --------------------------------------------------------------------
  1480.  
  1481.